package com.jn.jetpack.common;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.jn.jetpack.R;

/**
 * 自定义控件——选择dialog。
 */
public class ChooseDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private View mView;
    private TextView mTitle;
    private NumberPicker mNumberPicker;
    private Button btn1;
    private Button btn2;
    private OnButtonClickListener mListener;

    public ChooseDialog(Context context, OnButtonClickListener listener) {
        super(context);
        mContext = context;
        mListener = listener;
        initView();
        initListener();
        setCanceledOnTouchOutside(false);
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.choose_dialog, null);
        mTitle = mView.findViewById(R.id.biaoti);
        mNumberPicker = mView.findViewById(R.id.dp);
        btn1 = mView.findViewById(R.id.gbbtn);
        btn2 = mView.findViewById(R.id.qrbtn);
        getWindow().setContentView(mView);
    }

    public void setBetween(int start, int end) {
        mNumberPicker.setMinValue(start);
        mNumberPicker.setMaxValue(end);
    }

    public void setCurrent(int now) {
        mNumberPicker.setValue(now);
    }

    public void setText(String[] text) {
        mNumberPicker.setDisplayedValues(text);
        setBetween(0, text.length - 1);
    }

    private void initListener() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.gbbtn) {
            if (mListener != null) {
                mListener.close();
            }
        } else if (id == R.id.qrbtn) {
            if (mListener != null) {
                String[] values = mNumberPicker.getDisplayedValues();
                int value = mNumberPicker.getValue();
                if (values == null) {
                    mListener.confirm(String.valueOf(value));
                } else {
                    mListener.confirm(String.valueOf(values[value]));
                }
            }
        }
        dismiss();
    }

    public void setTitle(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    @Override
    public void show() {
        if (!isShowing()) {
            Window window = getWindow();
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.gravity = Gravity.TOP;
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            layoutParams.x = 0;
            layoutParams.y = 200;
            window.setAttributes(layoutParams);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            super.show();
        }
    }

    @Override
    public void dismiss() {
        if (isShowing()) {
            if (null != mContext) {
                super.dismiss();
            }
        }
    }

    public interface OnButtonClickListener {
        void confirm(String address);

        void close();
    }
}